/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Created on   : 11 Oct 2012
 * Creator      : Liang Quan
 * Description  : NumberField to be used with a Pagination Toolbar. 
 * @Parameters  : numberFieldId, a id to the numberfield to be created
 *                paginationId, the id of the pagination toolbar to embed the numberfield
 *                defaultRecordPerPage, the default number of records per page when grid first render
 *                gridId, id of the grid using the recordPerPgNumber
 *                editorIdToStopEditOpt(*optional), id of RowEditor if used with Grid
 * @Usage        : 1. Create a variable to intialize the following NumberField, 
 *                  e.g. var eoRecordPerPage = recordPerPgNumberField('eoRecordPerPage','eoPagination');
 *                2. Define the pagination toolbar pageSize with the following NumberField's value
 *                  e.g. pageSize: eoRecordPerPage.getValue(),
 *                3. Define additional item in the Pagination Toolbar
 *                  e.g. items: ['-','Records per Page:',eoRecordPerPage],
 */
function recordPerPgNumberField(numberFieldId, gridId, paginationId, defaultRecordPerPage, editorIdToStopEditOpt)
{
     var recordPerPage = new Ext.form.NumberField
    ({
        id: numberFieldId,
        name: numberFieldId,
        fieldLabel: 'Records/Page',
        value: defaultRecordPerPage,
        width: 30,
        enableKeyEvents: true
    })
    
    recordPerPage.on('keypress', function(){
        setRecordPerPage.delay(1000);
    })    
    
    var setRecordPerPage = new Ext.util.DelayedTask(function(){
        
        // loop arr of editor Id to stop edit
        if(editorIdToStopEditOpt && Ext.ComponentMgr.get(editorIdToStopEditOpt).isVisible())
        {
            Ext.ComponentMgr.get(editorIdToStopEditOpt).stopEditing(false);
//            for(var i=0; i< editorIdToStopEditOpt.length; i++)
//            {
//                if (Ext.ComponentMgr.get(editorIdToStopEditOpt[i]).isVisible())
//                {
//                    Ext.ComponentMgr.get(editorIdToStopEditOpt[i]).stopEditing(false);
//                }
//            }
        }
        
        if(recordPerPage.getValue())
        {
            var newPageSize = parseInt(recordPerPage.getValue());
            var pagination = Ext.ComponentMgr.get(paginationId);
            var grid = Ext.ComponentMgr.get(gridId);
            var selRecord = grid.getSelectionModel().getSelected();
            
            var gotoPage;
            var curPage = Math.ceil((pagination.cursor + pagination.pageSize) / pagination.pageSize); // current Page
            
            if(selRecord)
            {
                var pageRowIndex = grid.getStore().indexOf(selRecord);
                var storeRowIndex = ((curPage-1) * pagination.pageSize) + (pageRowIndex + 1);
                
                gotoPage = Math.ceil(storeRowIndex/newPageSize);
            }
            else
            {
                gotoPage = curPage;
            }
            pagination.pageSize = newPageSize;
            pagination.changePage(gotoPage);
            //pagination.doLoad(pagination.cursor);
        }
    })
    
    return recordPerPage;
}